
import os


class ResultValue:
    UNKNOWN = 0
    SUCCESS = 1
    FAIL = 2
    IGNORE = 3

    _RESULT = {
        UNKNOWN: 'Unknown',
        SUCCESS: 'Success',
        FAIL: 'Fail',
        IGNORE: 'Ignored'
    }

    def __init__(self, init=0):
        self.result = init

    def get(self):
        return self.result

    def set(self, value):
        if value in ResultValue._RESULT:
            self.result = value
        else:
            raise Exception('unknown value ' + str(value))

    def __repr__(self):
        return ResultValue._RESULT[self.result]


class Result:
    def __init__(self, resultset, case):
        self.case = case
        self.resultset = resultset
        self.result = ResultValue()

    def __str__(self):
        return 'Case "%s": %s' % (self.case, self.result)

    def __repr__(self):
        ret = '%s: %s' % (self.resultset.getName(), self.case)
        ret += '-------------------------------------------'
        ret += self.__str__()

        return ret

    def getResult(self):
        return self.result

    def assert_(self, value1, value2):
        if value1 == value2:
            self.result.set(ResultValue.SUCCESS)
        else:
            self.result.set(ResultValue.FAIL)

        return value1 == value2

    def assertTrue(self, result):
        return self.assert_(result, True)

    def assertFail(self, result):
        return self.assert_(result, False)

    def assertIgnore(self, result):
        if result:
            self.result.set(ResultValue.IGNORE)

        return result


class Results:
    def __init__(self, filename):
        self.filename = filename
        self.results = list()

    def __str__(self):
        ret = ''

        for result in self.results:
            ret += str(result) + os.linesep

        ret += os.linesep
        ret += 'Success: %d, Fail: %d, Ignore: %d' % self.getResult()
        return ret

    def __repr__(self):
        ret = self.filename
        ret += '======================================'
        ret += self.__str__()

        return ret

    def addCase(self, case):
        self.results.append(Result(self.filename, case))

        return self.results[-1]

    def getResult(self):
        success, fail, ignore = 0, 0, 0

        for result in self.results:
            value = result.getResult().get()
            if value == ResultValue.SUCCESS:
                success += 1
            elif value == ResultValue.IGNORE:
                ignore += 1
            else:  # if value == ResultValue.FAIL:
                fail += 1

        return success, fail, ignore


class Resultset:
    def __init__(self):
        self.sets = dict()
        self.resultss = list()

    def __str__(self):
        success, fail, ignore = 0, 0, 0

        for results in self.resultss:
            result = results.getResult()

            success += result[0]
            fail += result[1]
            ignore += result[2]

        return 'File: %d, Success: %d, Fail: %d, Ignore: %d' % (
            len(self.resultss), success, fail, ignore)

    def addFile(self, filename):
        self.resultss.append(Results(filename))

        return self.resultss[-1]
